package com.jk.hly.login.controller;

import com.jk.hly.login.bean.HlyUser;
import com.jk.hly.login.bean.Result;
import com.jk.hly.login.service.HlyLoginService;
import com.jk.hly.login.util.Constant;
import com.jk.hly.login.util.HttpClient;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("userLogin")
public class HlyLoginController {

    @Resource
    private HlyLoginService loginService;

    @ResponseBody
    @RequestMapping("zhangHaoAndPasswordLogin")
    public String zhangHaoAndPasswordLogin(@RequestBody HlyUser user) {

        HlyUser us = loginService.zhangHaoAndPasswordLogin(user);
        if (us == null) {
            return "1";
        }
        HlyUser usBlack = loginService.zhangHaoIsBlackList(user);
        if (usBlack == null) {
            return "2";
        }
        return "3";
    }

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    GregorianCalendar ming = new GregorianCalendar();

    @ResponseBody
    @RequestMapping("phoneLogin")
    public Result phoneLogin(@RequestBody String phone) {
        Result res = new Result();
        boolean hasKey = redisTemplate.hasKey(Constant.multiple_code + phone);
        boolean coun = redisTemplate.hasKey(Constant.count_code + phone);
        //hasKey是true代表redis中 当前key 一分钟内发过验证码了
        if (hasKey) {
            res.setCode(1);
        } else {
            int num = (int) ((Math.random() * 899999) + 100000);
            redisTemplate.opsForValue().set(Constant.phone_code + phone, num + "", 1, TimeUnit.MINUTES);

            //如果coun是false 获取第一次存值时的 明天的时间 ming 并存入申请次数
            if (!coun) {
                Date date = new Date();
                ming.setTime(date);
                ming.add(ming.MINUTE, 5);
                redisTemplate.opsForValue().set(Constant.count_code + phone, "0");
            }
            Date date3 = new Date();
            boolean bo = ming.getTime().before(date3);
            System.out.println(date3 + "-----1");
            System.out.println(ming + "-----2");

            if (bo) {
                redisTemplate.delete(Constant.count_code + phone);
                Date date5 = new Date();
                ming.setTime(date5);
                ming.add(ming.MINUTE, 5);
                redisTemplate.opsForValue().set(Constant.count_code + phone, "0");
            }
            //str是当日申请的次数
            String str = redisTemplate.opsForValue().get(Constant.count_code + phone);

            if (!str.equals("3")) {
                res.setCode(2);
                HashMap<String, Object> hashMap = new HashMap<String, Object>();

                hashMap.put("key", "a3c3179772c4854d6b832bad2c3157a5");
                hashMap.put("mobile", phone);
                hashMap.put("tpl_id", "124673");
                hashMap.put("tpl_value", "%23code%23%3d" + num);
                System.out.println(num + "-------");
               // HttpClient.sendGet("http://v.juhe.cn/sms/send",hashMap);

                int ss = Integer.valueOf(str) + 1;
                // System.out.println(ss+"==");
                redisTemplate.opsForValue().set(Constant.count_code + phone, ss + "");
                redisTemplate.opsForValue().set(Constant.multiple_code + phone, "jk", 60, TimeUnit.SECONDS);

            } else {
                res.setCode(4);
            }
        }
        return res;
    }


      @ResponseBody
      @RequestMapping("userPhoneLogin")
      public String userPhoneLogin(@RequestBody HlyUser  uss, HttpSession session){
            String phone=uss.getPhone();
            String  yzm=uss.getYzm();
          String code = redisTemplate.opsForValue().get(Constant.phone_code+phone);
          if(code!=null && code.equals(yzm)){
              HlyUser  us=loginService.userPhoneLogin(phone);
              if(us!=null){
                  session.setAttribute("user", us);
                  return  "1";
              }
              HlyUser  user=loginService.userPhoneLoginBlack(phone);
              if(us==null && user!=null){
                  return "4";
              }
              return  "2";
          }

          return "3";
      }

}
